package com.hamster.crmpre.web;

import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.pojo.PotentialCustomer;
import com.hamster.crmpre.service.PotentialCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 手机端对应的潜在客户的功能Controller
 * @author Tarry
 * @create 2019/12/21 10:40
 */
@RestController
@RequestMapping("potentialCustomer")
public class PotentialCustomerController {
    @Autowired
    private PotentialCustomerService potentialCustomerService;

    /**
     * 添加潜在客户
     *
     * @param potentialCustomer
     * @return
     */
    @PostMapping("insert")
    public ResponseEntity<Void> insert(@RequestBody PotentialCustomer potentialCustomer) {
        String id = LoginInterceptor.getLoginUser().getUserId(); //当前登录员工的id
        potentialCustomerService.insert(id, potentialCustomer);
        return ResponseEntity.ok().build();
    }

    /**
     * 事业部员工为分公司添加潜在客户
     *
     * @param id                分公司的id
     * @param potentialCustomer 潜在客户实体类
     * @return
     */
    @PostMapping("companyInsert")
    public ResponseEntity<Void> insert(@RequestParam String id, @RequestBody PotentialCustomer potentialCustomer) {
        potentialCustomerService.insert(id, potentialCustomer);
        return ResponseEntity.ok().build();
    }

    /**
     * 事业部员工  替分公司跟踪潜在客户
     *
     * @param companyId           分公司id
     * @param potentialCustomerId 潜在客户id
     * @return
     */
    @GetMapping("companyTrace")
    public ResponseEntity<Void> companyTrace(@RequestParam String companyId, @RequestParam String potentialCustomerId) {
        potentialCustomerService.trace(companyId, potentialCustomerId);
        return ResponseEntity.ok().build();
    }


    /**
     * 校验该客户是否已经录入。
     * 已录入 抛异常
     * 已转正 抛异常
     * 未录入，状态码200
     *
     * @param phone
     * @return
     */
    @GetMapping("verify")
    public ResponseEntity<Void> verify(@RequestParam String phone) {
        potentialCustomerService.verify(phone);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据潜在客户id跟踪潜在客户
     *
     * @param potentialCustomerId 潜在客户的id
     * @return 状态码 200
     */
    @GetMapping("trace")
    public ResponseEntity<Void> trace(@RequestParam String potentialCustomerId) {
        String id = LoginInterceptor.getLoginUser().getUserId(); //当前登录员工的id
        potentialCustomerService.trace(id, potentialCustomerId);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询潜在客户列表【只显示未转正的】
     *
     * @return List<PotentialCustomer> 潜在客户列表
     */
    @GetMapping("listPotentialCustomers")
    public ResponseEntity<List<PotentialCustomer>> listPotentialCustomers(@RequestParam(required = false) String name) {
        String id = LoginInterceptor.getLoginUser().getUserId(); //当前登录员工的id
        List<PotentialCustomer> list = potentialCustomerService.listPotentialCustomers(id,name);
        return ResponseEntity.ok(list);
    }


    /**
     * 为分公司查询当前潜在客户列表
     *
     * @param companyId 分公司id
     * @return
     */
    @GetMapping("companyListPotentialCustomers")
    public ResponseEntity<List<PotentialCustomer>> CompanyListPotentialCustomers(@RequestParam String companyId) {

        List<PotentialCustomer> list = potentialCustomerService.listPotentialCustomers(companyId,"");
        return ResponseEntity.ok(list);
    }
}